﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Data.Common;
using System.IO;
using System.Configuration;
using Top.Api.Request;
using Top.Api.Response;
using Top.Api.Domain;
using System.Text.RegularExpressions;
using Taoke.Core;
using Nop.Core;


namespace Taoke.Services
{
    /// <summary>
    ///TaoBaoKeService 的摘要说明
    /// </summary>
    public class TaoBaoKeService
    {
        private static TaoBaoKeService _TaoBaoKeService = null;
        private static object _ojbLock = new object();

        /// <summary>
        /// 获取数据服务实例
        /// </summary>
        public static TaoBaoKeService GetInstance
        {
            get
            {
                if (_TaoBaoKeService == null)
                {
                    lock (_ojbLock)
                    {
                        if (_TaoBaoKeService == null)
                        {
                            _TaoBaoKeService = new TaoBaoKeService();
                        }
                    }
                }
                return _TaoBaoKeService;
            }
        }

        /// <summary>
        /// 查询淘宝客推广商品
        /// </summary>
        /// <param name="sType">网站栏目类型</param>
        /// <param name="cid">商品类别id</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IPagedList<TaobaokeItem> GetItems(long categoryId, string keyword, string sort, string startPrice, string endPrice, int pageIndex, int pageSize)
        {
            TaobaokeItemsGetResponse crsp = new TaobaokeItemsGetResponse();
            TaobaokeItemsGetRequest creq = new TaobaokeItemsGetRequest();
            creq.Fields = "num_iid,title,nick,pic_url,price,click_url,commission,commission_rate,commission_num,commission_volume,shop_click_url,seller_credit_score,item_location,volume,coupon_price,coupon_start_time,coupon_end_time";
            creq.Nick = TOPHelper.Nick;
            creq.Cid = categoryId;
            creq.Sort = sort != null ? sort : "commissionNum_desc";
            creq.Keyword = keyword != null ? HttpContext.Current.Server.UrlDecode(keyword) : string.Empty ;
            if (startPrice != null && endPrice != null)
            {
                creq.StartPrice = startPrice;
                creq.EndPrice = endPrice;
            }
            creq.StartCommissionRate = TOPHelper.StartCommissionRate;
            creq.EndCommissionRate = TOPHelper.EndCommissionRate;
            creq.StartCredit = TOPHelper.StartCredit;
            creq.EndCredit = TOPHelper.EndCredit;
            creq.PageNo = pageIndex ;
            creq.PageSize = (long)pageSize;
            try
            {
                crsp = TOPHelper.Client.Execute(creq);
            }
            catch (Exception e)
            {
                
            }

            List<TaobaokeItem> list = crsp.TaobaokeItems;
            long totalCount = crsp.TotalResults;
            var ret = new PagedList<TaobaokeItem>(list,pageIndex,pageSize,(int)totalCount);
            return ret;
        }

        public IPagedList<TaobaokeItem> GetCouponItems(long categoryId, string keyword, string sort, int pageIndex, int pageSize)
        {
            TaobaokeItemsCouponGetResponse crsp = new TaobaokeItemsCouponGetResponse();
            TaobaokeItemsCouponGetRequest creq = new TaobaokeItemsCouponGetRequest();
            creq.Fields = "num_iid,title,nick,pic_url,price,click_url,commission,commission_rate,commission_num,commission_volume,shop_click_url,seller_credit_score,item_location,volume,coupon_price,coupon_start_time,coupon_end_time";
            creq.Nick = TOPHelper.Nick;
            if (categoryId < 0)
            {
                categoryId = TOPHelper.Categry_Women;
            }
            creq.Cid = categoryId;
            creq.Sort = sort != null ? sort : "commissionNum_desc";
            creq.Keyword = keyword != null ? HttpContext.Current.Server.UrlDecode(keyword) : string.Empty;
            creq.StartCommissionRate = 1000;
            creq.EndCommissionRate = 5000;
            creq.StartCredit = TOPHelper.StartCredit;
            creq.EndCredit = TOPHelper.EndCredit;
            creq.PageNo = pageIndex ;
            creq.PageSize = (long)pageSize;
            try
            {
                crsp = TOPHelper.Client.Execute(creq);
            }
            catch (Exception e)
            {

            }

            List<TaobaokeItem> list = crsp.TaobaokeItems;
            long totalCount = crsp.TotalResults;
            var ret = new PagedList<TaobaokeItem>(list, pageIndex, pageSize, (int)totalCount);
            return ret;
        }

        public TaobaokeItemDetail GetTaokeItemDetail(string numIid)
        {
            TaobaokeItemsDetailGetResponse crsp = new TaobaokeItemsDetailGetResponse();
            TaobaokeItemsDetailGetRequest creq = new TaobaokeItemsDetailGetRequest();
            creq.Fields = "num_iid,click_url,shop_click_url,seller_credit_score,price,cid,title,desc,num,volume,pic_url,list_time,delist_time,post_fee,express_fee,ems_fee,freight_payer,input_str,props_name";
            creq.Nick = TOPHelper.Nick;
            creq.NumIids = numIid;
            string wapdesc = string.Empty;
            if (HttpContext.Current.Request.Browser.IsMobileDevice)
            {
                creq.IsMobile =true;
                ItemGetResponse crspitem = new ItemGetResponse();
                ItemGetRequest creqitem = new ItemGetRequest();
                creqitem.Fields = "num_iid,wap_desc  ";
                long id=0;
                long.TryParse(numIid,out id);
                creqitem.NumIid = id;
                try
                {
                    crspitem = TOPHelper.Client.Execute(creqitem);
                    wapdesc = crspitem.Item.WapDesc;
                }
                catch (Exception e)
                {

                }
            }
            try
            {
                crsp = TOPHelper.Client.Execute(creq);
            }
            catch (Exception e)
            {

            }
            if (crsp.TaobaokeItemDetails.Count > 0)
            {
                if (!string.IsNullOrEmpty(wapdesc))
                {
                    crsp.TaobaokeItemDetails[0].Item.WapDesc = wapdesc;
                }
                return crsp.TaobaokeItemDetails[0];
            }
            return null;
        }


        public Item GetItem(long numIid)
        {
            ItemGetResponse crsp = new ItemGetResponse();
            ItemGetRequest creq = new ItemGetRequest();
            creq.Fields = "num_iid,cid,title,desc,num,list_time,delist_time,post_fee,express_fee,ems_fee,freight_payer";
            creq.NumIid = numIid;
            try
            {
                crsp = TOPHelper.Client.Execute(creq);
            }
            catch (Exception e)
            {
                
            }
            return crsp.Item;
        }

        public ItemCat GetItemCat(string cid)
        {
            ItemCat cat = new ItemCat();
            ItemcatsGetResponse crsp = new ItemcatsGetResponse();
            ItemcatsGetRequest creq = new ItemcatsGetRequest();
            creq.Fields = "cid,parent_cid,name,is_parent,status,sort_order";
            creq.Cids = cid;
            try
            {
                crsp = TOPHelper.Client.Execute(creq);
            }
            catch (Exception e)
            {
                
            }
            if (crsp.ItemCats != null)
            {
                cat= crsp.ItemCats.Count > 0 ? crsp.ItemCats[0] : null;
            }
            return cat;
        }

        public List<ItemCat> GetItemCatList(string cids)
        {
            ItemcatsGetResponse crsp = new ItemcatsGetResponse();
            ItemcatsGetRequest creq = new ItemcatsGetRequest();
            creq.Fields = "";
            creq.Cids = cids;
            try
            {
                crsp = TOPHelper.Client.Execute(creq);
            }
            catch (Exception e)
            {
                
            }
            return crsp.ItemCats;
        }

        /// <summary>
        /// 查询类别列表
        /// </summary>
        /// <param name="pcid">低级类目ID</param>
        /// <returns></returns>
        public List<ItemCat> GetItemCatList(long parentCid)
        {
            ItemcatsGetResponse crsp = new ItemcatsGetResponse();
            ItemcatsGetRequest creq = new ItemcatsGetRequest();
            creq.Fields = "";
            creq.ParentCid = parentCid;
            try
            {
                crsp = TOPHelper.Client.Execute(creq);
            }
            catch (Exception e)
            {
                
            }
            return crsp.ItemCats;
        }

    }
}